package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.ctsi.ssdc.service.LitemallFootprintService;
import com.ctsi.ssdc.domain.LitemallFootprint;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallFootprint.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-footprint-controller"})
@RestController
@RequestMapping("/api")
public class LitemallFootprintController {

    private final Logger log = LoggerFactory.getLogger(LitemallFootprintController.class);

    private static final String ENTITY_NAME = "litemallFootprint";

    private final LitemallFootprintService litemallFootprintService;

    public LitemallFootprintController(LitemallFootprintService litemallFootprintService) {
        this.litemallFootprintService = litemallFootprintService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallFootprints : Create a new litemallFootprint.
     *
     * @param litemallFootprint the litemallFootprint to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallFootprint,
        or with status 400 (Bad Request) if the litemallFootprint has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallFootprint",name = "litemallFootprint",value = "the litemallFootprint to create")
    })
    @ApiOperation(value = "POST  /litemallFootprints : create a new litemallFootprint.",
        notes = "POST  /litemallFootprints : create a new litemallFootprint.",httpMethod = "POST")
    @PostMapping("/litemallFootprints")
    public ResponseEntity<LitemallFootprint> createLitemallFootprint(@RequestBody LitemallFootprint litemallFootprint) throws URISyntaxException {
        log.debug("REST request to save LitemallFootprint : {}", litemallFootprint);
        if (litemallFootprint.getId() != null) {
            throw new BadRequestAlertException("A new litemallFootprint cannot already have an id", ENTITY_NAME, "idexists");
        }

        boolean save =  litemallFootprintService.save(litemallFootprint);
        Assert.isTrue(save,"Insert failed");
        LitemallFootprint result=litemallFootprint;

        return ResponseEntity.created(new URI("/api/litemallFootprints" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallFootprints : Updates an existing litemallFootprint.
     *
     * @param litemallFootprint the litemallFootprint to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallFootprint,
     * or with status 400 (Bad Request) if the litemallFootprint is not valid,
     * or with status 500 (Internal Server Error) if the litemallFootprint couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallFootprint",name = "litemallFootprint",value = "the litemallFootprint to update")
    })
    @ApiOperation(value = "PUT  /litemallFootprints : updates an existing litemallFootprint.",
        notes = "PUT  /litemallFootprints : updates an existing litemallFootprint.",httpMethod = "PUT")
    @PutMapping("/litemallFootprints")
    public ResponseEntity<LitemallFootprint> updateLitemallFootprint(@RequestBody LitemallFootprint litemallFootprint) throws URISyntaxException {
        log.debug("REST request to update LitemallFootprint : {}", litemallFootprint);
	    if (litemallFootprint.getId() == null) {
	    	return createLitemallFootprint(litemallFootprint);
	    }

        boolean update = litemallFootprintService.updateById(litemallFootprint);
        Assert.isTrue(update,"Update failed");
        LitemallFootprint result=litemallFootprint;

        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallFootprints/:id : get the "id" litemallFootprint.
     *
     * @param id the id of the litemallFootprint to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallFootprint, or with status 404 (Not Found)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
        name = "id",value = "the id of the litemallFootprint to retrieve")
    })
    @ApiOperation(value = "GET  /litemallFootprints/id : get the id litemallFootprint.",
        notes = "GET  /litemallFootprints/id : get the id litemallFootprint.",httpMethod = "GET")
    @GetMapping("/litemallFootprints/{id}")
    public ResponseEntity<LitemallFootprint> getLitemallFootprint(@PathVariable Integer id) {
        log.debug("REST request to get LitemallFootprint : {}", id);
        LitemallFootprint litemallFootprint = litemallFootprintService.getById(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallFootprint));
    }

    /**
    * GET  /litemallFootprints : get the litemallFootprints firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallFootprints in body
    */
    @ApiOperation(value = "GET  /litemallFootprints/id : get the id litemallFootprint.",
        notes = "GET  /litemallFootprints/id : get the id litemallFootprint.",httpMethod = "GET")
    @GetMapping("/litemallFootprints")
    public PageResult<LitemallFootprint> getLitemallFootprintsList(String str,Pageable pageable) {
        log.debug("REST request to get LitemallFootprintsList");
        return litemallFootprintService.findFirstStringColumn(str,pageable);
    }
	
    /**
     * DELETE  /litemallFootprints/:id : delete the "id" litemallFootprint.
     *
     * @param id the id of the litemallFootprint to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
        value = "the id of the litemallFootprint to delete")
    })
    @ApiOperation(value = "DELETE  /litemallFootprints/id : delete the id litemallFootprint.",
        notes = "DELETE  /litemallFootprints/id : delete the id litemallFootprint.",httpMethod = "DELETE")
    @DeleteMapping("/litemallFootprints/{id}")
    public ResponseEntity<Void> deleteLitemallFootprint(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallFootprint : {}", id);
        litemallFootprintService.removeById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
